/*
 * This file is part of the Gwt-Generator project and was written by Henri Darmet for Objet Direct
 * <http://wwww.objetdirect.com>
 * 
 * Copyright © 2009 Objet Direct
 * 
 * Gwt-Generator is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * Gwt-Generator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with Gwt-Generator. If not, see <http://www.gnu.org/licenses/>.
 */

package com.objetdirect.seam;

import com.objetdirect.engine.ScriptDescriptor;
import com.objetdirect.engine.TestUtil;
import com.objetdirect.entities.EntityDescriptor;
import com.objetdirect.seam.lists.PersistentSelectManyListDescriptor;
import com.objetdirect.seam.lists.SelectManyDetailDescriptor;
import com.objetdirect.seam.lists.TransientSelectManyListDescriptor;

import junit.framework.TestCase;

public class TestPersistentSelectManyListPage extends TestCase {

	public void testPersistentListWithReverseRelationship() {
		Seam.clear();
		EntityDescriptor category = new EntityDescriptor("com.objetdirect.domain", "Category").
			addStringField("name", null);
		EntityDescriptor skill = new EntityDescriptor("com.objetdirect.domain", "Skill").
			addStringField("name", null);
		category.addOneToMany(skill, "skills", true, "category", true, true);
		PageDescriptor page = 
			new PageDescriptor("com.objetdirect.actions", "EditSkills", "views", "edit-skills");
		TransientSelectManyListDescriptor listFeature = new TransientSelectManyListDescriptor(category).
			showField("name", "Name", 20).
			addDelete("Voulez-vous supprimer la categorie : #{categoryToDelete.name} ?");
		SelectManyDetailDescriptor detail = new SelectManyDetailDescriptor().addCreate(
			new ScriptDescriptor(
				"return new Category(\"Nom de la categorie\");"
			).replace("Category", category.getClassDescriptor())
		);
		detail.addForm(new FormDescriptor().
			editStringField("name", "Nom de la categorie", 30)
		);
		PersistentSelectManyListDescriptor persistentList = new PersistentSelectManyListDescriptor("skills")
			.showField("name", "Name", 20)
			.addDelete("Voulez-vous supprimer la competence : #{skillToDelete.name} ?");
		SelectManyDetailDescriptor skillDetail = new SelectManyDetailDescriptor().addCreate(
			new ScriptDescriptor(
				"return new Skill(\"Nom de la competence\");"
			).replace("Skill", skill.getClassDescriptor())
		);
		skillDetail.addForm(new FormDescriptor().
			editStringField("name", "Nom de la competence", 30)
		);
		persistentList.setDetail(skillDetail);
		detail.addPersistentList(persistentList);
		listFeature.setDetail(detail);
		page.setFeature(listFeature);
		page.build();
		// java
		TestUtil.assertText(page.getJavaText(),
			"package com.objetdirect.actions;",
			"",
			"import com.objetdirect.domain.Category;",
			"import com.objetdirect.domain.Skill;",
			"import com.objetdirect.jsffrmk.GuiUtil;",
			"import java.util.ArrayList;",
			"import java.util.HashMap;",
			"import java.util.List;",
			"import java.util.Map;",
			"import javax.faces.context.FacesContext;",
			"import javax.persistence.EntityManager;",
			"import org.jboss.seam.annotations.In;",
			"import org.jboss.seam.annotations.Name;",
			"import org.jboss.seam.annotations.Scope;",
			"import org.jboss.seam.core.Conversation;",
			"import org.jboss.seam.ScopeType;",
			"",
			"@Name(\"editSkills\")",
			"@Scope(ScopeType.CONVERSATION)",
			"public class EditSkillsAnimator {",
			"",
			"	@In",
			"	EntityManager entityManager;",
			"	List<Category> categories = null;",
			"	Category targetCategoryForDeletion = null;",
			"	Map<Category, Boolean> selectedCategories = new HashMap<Category, Boolean>();",
			"	List<Category> openedCategories = null;",
			"	int currentOpenedCategoriesIndex = 0;",
			"	Category currentCategory = null;",
			"	static final int CATEGORIES_FAST_STEP = 10;",
			"	static final int CATEGORIES_NO_NAVIGATION = 0;",
			"	static final int CATEGORIES_GO_FIRST = 1;",
			"	static final int CATEGORIES_GO_FAST_PREVIOUS = 2;",
			"	static final int CATEGORIES_GO_PREVIOUS = 3;",
			"	static final int CATEGORIES_GO_NEXT = 4;",
			"	static final int CATEGORIES_GO_FAST_NEXT = 5;",
			"	static final int CATEGORIES_GO_LAST = 6;",
			"	static final int CLOSE_CATEGORIES_POPUP = 7;",
			"	int categoriesNavigation = CATEGORIES_NO_NAVIGATION;",
			"	boolean isNewCategory = false;",
			"	boolean isRecentCategory = false;",
			"	static final int CREATE_CATEGORY = 8;",
			"	String currentCategoryName;",
			"	List<Skill> skills = null;",
			"	Skill targetSkillForDeletion = null;",
			"	Map<Skill, Boolean> selectedSkills = new HashMap<Skill, Boolean>();",
			"	List<Skill> openedSkills = null;",
			"	int currentOpenedSkillsIndex = 0;",
			"	Skill currentSkill = null;",
			"	static final int SKILLS_FAST_STEP = 10;",
			"	static final int SKILLS_NO_NAVIGATION = 0;",
			"	static final int SKILLS_GO_FIRST = 1;",
			"	static final int SKILLS_GO_FAST_PREVIOUS = 2;",
			"	static final int SKILLS_GO_PREVIOUS = 3;",
			"	static final int SKILLS_GO_NEXT = 4;",
			"	static final int SKILLS_GO_FAST_NEXT = 5;",
			"	static final int SKILLS_GO_LAST = 6;",
			"	static final int CLOSE_SKILLS_POPUP = 7;",
			"	int skillsNavigation = SKILLS_NO_NAVIGATION;",
			"	boolean isNewSkill = false;",
			"	static final int CREATE_SKILL = 8;",
			"	String currentSkillName;",
			"	static final int OP_NONE = 0;",
			"	static final int OP_DELETE = 1;",
			"	int operation = OP_NONE;",
			"",
			"	public EditSkillsAnimator() {",
			"		if (Conversation.instance().isLongRunning()) {",
			"			Conversation.instance().end(true);",
			"			Conversation.instance().leave();",
			"		}",
			"		Conversation.instance().begin();",
			"	}",
			"",
			"	@SuppressWarnings(\"unchecked\")",
			"	public List<Category> getCategories() {",
			"		if (categories==null) {",
			"			categories = entityManager.createQuery(\"from Category\").getResultList();",
			"			selectedCategories = new HashMap<Category, Boolean>();",
			"		}",
			"		return categories;",
			"	}",
			"	",
			"	public void requestCategoryDeletion(Category category) {",
			"		targetCategoryForDeletion = category;",
			"		operation = OP_DELETE;",
			"	}",
			"	",
			"	public Category getTargetCategoryForDeletion() {",
			"		return targetCategoryForDeletion;",
			"	}",
			"	",
			"	void deleteCategory(Category category) {",
			"		categories.remove(category);",
			"		entityManager.remove(category);",
			"		selectedCategories.remove(category);",
			"		if (category==currentCategory)",
			"			currentCategory = null;",
			"	}",
			"	",
			"	public Map<Category, Boolean> getSelectedCategories() {",
			"		return selectedCategories;",
			"	}",
			"	",
			"	public Category getCurrentCategory() {",
			"		return currentCategory;",
			"	}",
			"	",
			"	void setCurrentCategory(int selectedIndex) {",
			"		if (openedCategories.size()==0)",
			"			return;",
			"		currentCategory = openedCategories.get(index);",
			"		currentCategoryName = currentCategory.getName();",
			"		currentSkill = null;",
			"	}",
			"	",
			"	public boolean isCurrentCategoryVisible() {",
			"		return currentCategory != null;",
			"	}",
			"	",
			"	public void setCurrentCategoryVisible(boolean visible) {",
			"	}",
			"	",
			"	void clearCurrentCategory() {",
			"		openedCategories = null;",
			"		currentCategory = null;",
			"		isNewCategory = false;",
			"		isRecentCategory = false;",
			"		currentSkill = null;",
			"	}",
			"	",
			"	boolean isCurrentCategoryValid() {",
			"		return !FacesContext.getCurrentInstance().getMessages().hasNext();",
			"	}",
			"	",
			"	public void cancelCurrentCategory() {",
			"		GuiUtil.cancelGui();",
			"		if (isNewCategory) {",
			"			openedCategories.remove(currentCategory);",
			"			if (currentOpenedCategoriesIndex>=openedCategories.size())",
			"				currentOpenedCategoriesIndex = openedCategories.size()-1;",
			"			isNewCategory = false;",
			"			isRecentCategory = false;",
			"		}",
			"		if (openedCategories.size()==0)",
			"			clearCurrentCategory();",
			"		else",
			"			setCurrentCategory(currentOpenedCategoriesIndex);",
			"	}",
			"	",
			"	public void validateCurrentCategory() {",
			"		if (isCurrentCategoryValid()) {",
			"			currentCategory.setName(currentCategoryName);",
			"			if (isNewCategory) {",
			"				entityManager.persist(currentCategory);",
			"				categories.add(currentCategory);",
			"				isNewCategory = false;",
			"			}",
			"			return true;",
			"		}",
			"		else",
			"			return false;",
			"	}",
			"	",
			"	boolean isCurrentCategoryDirty() {",
			"		if(isNewCategory)",
			"			return true;",
			"		if (currentCategory==null)",
			"			return false;",
			"		if (!GuiUtil.equals(currentCategoryName, currentCategory.getName()))",
			"			return true;",
			"		return false;",
			"	}",
			"	",
			"	public boolean isConfirmCategoriesNavigationRequested() {",
			"		return categoriesNavigation!=CATEGORIES_NO_NAVIGATION;",
			"	}",
			"	",
			"	public void cancelCategoriesNavigation() {",
			"		categoriesNavigation = CATEGORIES_NO_NAVIGATION;",
			"	}",
			"	",
			"	void doCategoriesGoFirst() {",
			"		currentOpenedCategoriesIndex = 0;",
			"		setCurrentCategory(currentOpenedCategoriesIndex);",
			"	}",
			"	",
			"	public void categoriesGoFirst() {",
			"		if (isCurrentCategoryDirty())",
			"			categoriesNavigation = CATEGORIES_GO_FIRST;",
			"		else",
			"			doCategoriesGoFirst();",
			"	}",
			"	",
			"	void doCategoriesGoFastPrevious() {",
			"		currentOpenedCategoriesIndex -= CATEGORIES_FAST_STEP;",
			"		if (currentOpenedCategoriesIndex<0)",
			"			currentOpenedCategoriesIndex=0;",
			"		setCurrentCategory(currentOpenedCategoriesIndex);",
			"	}",
			"	",
			"	public void categoriesGoFastPrevious() {",
			"		if (isCurrentCategoryDirty())",
			"			categoriesNavigation = CATEGORIES_GO_FAST_PREVIOUS;",
			"		else",
			"			doCategoriesGoFastPrevious();",
			"	}",
			"	",
			"	void doCategoriesGoPrevious() {",
			"		currentOpenedCategoriesIndex -= 1;",
			"		if (currentOpenedCategoriesIndex<0)",
			"			currentOpenedCategoriesIndex=0;",
			"		setCurrentCategory(currentOpenedCategoriesIndex);",
			"	}",
			"	",
			"	public void categoriesGoPrevious() {",
			"		if (isCurrentCategoryDirty())",
			"			categoriesNavigation = CATEGORIES_GO_PREVIOUS;",
			"		else",
			"			doCategoriesGoPrevious();",
			"	}",
			"	",
			"	void doCategoriesGoNext() {",
			"		currentOpenedCategoriesIndex += 1;",
			"		if (currentOpenedCategoriesIndex>=openedCategories.size())",
			"			currentOpenedCategoriesIndex=openedCategories.size()-1;",
			"		setCurrentCategory(currentOpenedCategoriesIndex);",
			"	}",
			"	",
			"	public void categoriesGoNext() {",
			"		if (isCurrentCategoryDirty())",
			"			categoriesNavigation = CATEGORIES_GO_NEXT;",
			"		else",
			"			doCategoriesGoNext();",
			"	}",
			"	",
			"	void doCategoriesGoFastNext() {",
			"		currentOpenedCategoriesIndex += CATEGORIES_FAST_STEP;",
			"		if (currentOpenedCategoriesIndex>=openedCategories.size())",
			"			currentOpenedCategoriesIndex=openedCategories.size()-1;",
			"		setCurrentCategory(currentOpenedCategoriesIndex);",
			"	}",
			"	",
			"	public void categoriesGoFastNext() {",
			"		if (isCurrentCategoryDirty())",
			"			categoriesNavigation = CATEGORIES_GO_FAST_NEXT;",
			"		else",
			"			doCategoriesGoFastNext();",
			"	}",
			"	",
			"	void doCategoriesGoLast() {",
			"		currentOpenedCategoriesIndex = openedCategories.size()-1;",
			"		setCurrentCategory(currentOpenedCategoriesIndex);",
			"	}",
			"	",
			"	public void categoriesGoLast() {",
			"		if (isCurrentCategoryDirty())",
			"			categoriesNavigation = CATEGORIES_GO_LAST;",
			"		else",
			"			doCategoriesGoLast();",
			"	}",
			"	",
			"	protected Category buildCategory() {",
			"		return new Category(\"Nom de la categorie\");",
			"	}",
			"	",
			"	public void createCategory() {",
			"		openedCategories = new ArrayList<Category>();",
			"		openedCategories.add(buildCategory());",
			"		isNewCategory = true;",
			"		isRecentCategory = true;",
			"		categoriesGoFirst();",
			"	}",
			"	",
			"	void doInsertCategory() {",
			"		currentOpenedCategoriesIndex++;",
			"		Category category = buildCategory();",
			"		if (currentOpenedCategoriesIndex==openedCategories.size())",
			"			openedCategories.add(category);",
			"		else",
			"			openedCategories.add(currentOpenedCategoriesIndex, category);",
			"		isNewCategory = true;",
			"		setCurrentCategory(currentOpenedCategoriesIndex);",
			"	}",
			"	",
			"	public void insertCategory() {",
			"		if (isCurrentCategoryDirty())",
			"			categoriesNavigation = CREATE_CATEGORY;",
			"		else",
			"			doInsertCategory();",
			"	}",
			"	",
			"	public boolean isCreateCategoryRendered() {",
			"		return !isNewCategory;",
			"	}",
			"	",
			"	public void closeCurrentCategory() {",
			"		if (isCurrentCategoryDirty())",
			"			categoriesNavigation = CLOSE_CATEGORIES_POPUP;",
			"		else",
			"			clearCurrentCategory();",
			"	}",
			"	",
			"	void navigateInsideCategories() {",
			"		if (categoriesNavigation==CATEGORIES_GO_FIRST)",
			"			categoriesGoFirst();",
			"		else if (categoriesNavigation==CATEGORIES_GO_FAST_PREVIOUS)",
			"			categoriesGoFastPrevious();",
			"		else if (categoriesNavigation==CATEGORIES_GO_PREVIOUS)",
			"			categoriesGoPrevious();",
			"		else if (categoriesNavigation==CATEGORIES_GO_NEXT)",
			"			categoriesGoNext();",
			"		else if (categoriesNavigation==CATEGORIES_GO_FAST_NEXT)",
			"			categoriesGoFastNext();",
			"		else if (categoriesNavigation==CATEGORIES_GO_LAST)",
			"			categoriesGoLast();",
			"		else if (categoriesNavigation==CLOSE_CATEGORIES_POPUP)",
			"			closeCurrentCategory();",
			"		else if (categoriesNavigation==CREATE_CATEGORY)",
			"			doInsertCategory();",
			"		categoriesNavigation = CATEGORIES_NO_NAVIGATION;",
			"	}",
			"	",
			"	public void saveAndNavigateInsideCategories() {",
			"		validateCurrentCategory();",
			"		navigateInsideCategories();",
			"	}",
			"	",
			"	public void cancelAndNavigateInsideCategories() {",
			"		cancelCurrentCategory();",
			"		navigateInsideCategories();",
			"	}",
			"	",
			"	public String getCurrentCategoryName() {",
			"		return currentCategoryName;",
			"	}",
			"	",
			"	public void setCurrentCategoryName(String currentCategoryName) {",
			"		this.currentCategoryName = currentCategoryName;",
			"	}",
			"	",
			"	public boolean isCategorySkillsVisible() {",
			"		return currentCategory !=null;",
			"	}",
			"	",
			"	@SuppressWarnings(\"unchecked\")",
			"	public List<Skill> getSkills() {",
			"		if (skills!=currentCategory.getSkills()) {",
			"			skills = currentCategory.getSkills();",
			"			selectedSkills = new HashMap<Skill, Boolean>();",
			"		}",
			"		return skills;",
			"	}",
			"	",
			"	public void requestSkillDeletion(Skill skill) {",
			"		targetSkillForDeletion = skill;",
			"		operation = OP_DELETE;",
			"	}",
			"	",
			"	public Skill getTargetSkillForDeletion() {",
			"		return targetSkillForDeletion;",
			"	}",
			"	",
			"	void deleteSkill(Skill skill) {",
			"		skill.setCategory(null);",
			"		entityManager.remove(skill);",
			"		selectedSkills.remove(skill);",
			"		if (skill==currentSkill)",
			"			currentSkill = null;",
			"	}",
			"	",
			"	public Map<Skill, Boolean> getSelectedSkills() {",
			"		return selectedSkills;",
			"	}",
			"	",
			"	public Skill getCurrentSkill() {",
			"		return currentSkill;",
			"	}",
			"	",
			"	void setCurrentSkill(int selectedIndex) {",
			"		if (openedSkills.size()==0)",
			"			return;",
			"		currentSkill = openedSkills.get(index);",
			"		currentSkillName = currentSkill.getName();",
			"	}",
			"	",
			"	public boolean isCurrentSkillVisible() {",
			"		return isCategorySkillsVisible() && currentSkill != null;",
			"	}",
			"	",
			"	public void setCurrentSkillVisible(boolean visible) {",
			"	}",
			"	",
			"	void clearCurrentSkill() {",
			"		openedSkills = null;",
			"		currentSkill = null;",
			"		isNewSkill = false;",
			"	}",
			"	",
			"	boolean isCurrentSkillValid() {",
			"		return !FacesContext.getCurrentInstance().getMessages().hasNext();",
			"	}",
			"	",
			"	public void cancelCurrentSkill() {",
			"		GuiUtil.cancelGui();",
			"		if (isNewSkill) {",
			"			openedSkills.remove(currentSkill);",
			"			if (currentOpenedSkillsIndex>=openedSkills.size())",
			"				currentOpenedSkillsIndex = openedSkills.size()-1;",
			"			isNewSkill = false;",
			"		}",
			"		if (openedSkills.size()==0)",
			"			clearCurrentSkill();",
			"		else",
			"			setCurrentSkill(currentOpenedSkillsIndex);",
			"	}",
			"	",
			"	public void validateCurrentSkill() {",
			"		if (isCurrentSkillValid()) {",
			"			currentSkill.setName(currentSkillName);",
			"			if (isNewSkill) {",
			"				entityManager.persist(currentSkill);",
			"				currentSkill.setCategory(currentCategory);",
			"				isNewSkill = false;",
			"			}",
			"			return true;",
			"		}",
			"		else",
			"			return false;",
			"	}",
			"	",
			"	boolean isCurrentSkillDirty() {",
			"		if(isNewSkill)",
			"			return true;",
			"		if (currentSkill==null)",
			"			return false;",
			"		if (!GuiUtil.equals(currentSkillName, currentSkill.getName()))",
			"			return true;",
			"		return false;",
			"	}",
			"	",
			"	public boolean isConfirmSkillsNavigationRequested() {",
			"		return skillsNavigation!=SKILLS_NO_NAVIGATION;",
			"	}",
			"	",
			"	public void cancelSkillsNavigation() {",
			"		skillsNavigation = SKILLS_NO_NAVIGATION;",
			"	}",
			"	",
			"	void doSkillsGoFirst() {",
			"		currentOpenedSkillsIndex = 0;",
			"		setCurrentSkill(currentOpenedSkillsIndex);",
			"	}",
			"	",
			"	public void skillsGoFirst() {",
			"		if (isCurrentSkillDirty())",
			"			skillsNavigation = SKILLS_GO_FIRST;",
			"		else",
			"			doSkillsGoFirst();",
			"	}",
			"	",
			"	void doSkillsGoFastPrevious() {",
			"		currentOpenedSkillsIndex -= SKILLS_FAST_STEP;",
			"		if (currentOpenedSkillsIndex<0)",
			"			currentOpenedSkillsIndex=0;",
			"		setCurrentSkill(currentOpenedSkillsIndex);",
			"	}",
			"	",
			"	public void skillsGoFastPrevious() {",
			"		if (isCurrentSkillDirty())",
			"			skillsNavigation = SKILLS_GO_FAST_PREVIOUS;",
			"		else",
			"			doSkillsGoFastPrevious();",
			"	}",
			"	",
			"	void doSkillsGoPrevious() {",
			"		currentOpenedSkillsIndex -= 1;",
			"		if (currentOpenedSkillsIndex<0)",
			"			currentOpenedSkillsIndex=0;",
			"		setCurrentSkill(currentOpenedSkillsIndex);",
			"	}",
			"	",
			"	public void skillsGoPrevious() {",
			"		if (isCurrentSkillDirty())",
			"			skillsNavigation = SKILLS_GO_PREVIOUS;",
			"		else",
			"			doSkillsGoPrevious();",
			"	}",
			"	",
			"	void doSkillsGoNext() {",
			"		currentOpenedSkillsIndex += 1;",
			"		if (currentOpenedSkillsIndex>=openedSkills.size())",
			"			currentOpenedSkillsIndex=openedSkills.size()-1;",
			"		setCurrentSkill(currentOpenedSkillsIndex);",
			"	}",
			"	",
			"	public void skillsGoNext() {",
			"		if (isCurrentSkillDirty())",
			"			skillsNavigation = SKILLS_GO_NEXT;",
			"		else",
			"			doSkillsGoNext();",
			"	}",
			"	",
			"	void doSkillsGoFastNext() {",
			"		currentOpenedSkillsIndex += SKILLS_FAST_STEP;",
			"		if (currentOpenedSkillsIndex>=openedSkills.size())",
			"			currentOpenedSkillsIndex=openedSkills.size()-1;",
			"		setCurrentSkill(currentOpenedSkillsIndex);",
			"	}",
			"	",
			"	public void skillsGoFastNext() {",
			"		if (isCurrentSkillDirty())",
			"			skillsNavigation = SKILLS_GO_FAST_NEXT;",
			"		else",
			"			doSkillsGoFastNext();",
			"	}",
			"	",
			"	void doSkillsGoLast() {",
			"		currentOpenedSkillsIndex = openedSkills.size()-1;",
			"		setCurrentSkill(currentOpenedSkillsIndex);",
			"	}",
			"	",
			"	public void skillsGoLast() {",
			"		if (isCurrentSkillDirty())",
			"			skillsNavigation = SKILLS_GO_LAST;",
			"		else",
			"			doSkillsGoLast();",
			"	}",
			"	",
			"	protected Skill buildSkill() {",
			"		return new Skill(\"Nom de la competence\");",
			"	}",
			"	",
			"	public void createSkill() {",
			"		openedSkills = new ArrayList<Skill>();",
			"		openedSkills.add(buildSkill());",
			"		isNewSkill = true;",
			"		skillsGoFirst();",
			"	}",
			"	",
			"	void doInsertSkill() {",
			"		currentOpenedSkillsIndex++;",
			"		Skill skill = buildSkill();",
			"		if (currentOpenedSkillsIndex==openedSkills.size())",
			"			openedSkills.add(skill);",
			"		else",
			"			openedSkills.add(currentOpenedSkillsIndex, skill);",
			"		isNewSkill = true;",
			"		setCurrentSkill(currentOpenedSkillsIndex);",
			"	}",
			"	",
			"	public void insertSkill() {",
			"		if (isCurrentSkillDirty())",
			"			skillsNavigation = CREATE_SKILL;",
			"		else",
			"			doInsertSkill();",
			"	}",
			"	",
			"	public boolean isCreateSkillRendered() {",
			"		return !isNewSkill;",
			"	}",
			"	",
			"	public void closeCurrentSkill() {",
			"		if (isCurrentSkillDirty())",
			"			skillsNavigation = CLOSE_SKILLS_POPUP;",
			"		else",
			"			clearCurrentSkill();",
			"	}",
			"	",
			"	void navigateInsideSkills() {",
			"		if (skillsNavigation==SKILLS_GO_FIRST)",
			"			skillsGoFirst();",
			"		else if (skillsNavigation==SKILLS_GO_FAST_PREVIOUS)",
			"			skillsGoFastPrevious();",
			"		else if (skillsNavigation==SKILLS_GO_PREVIOUS)",
			"			skillsGoPrevious();",
			"		else if (skillsNavigation==SKILLS_GO_NEXT)",
			"			skillsGoNext();",
			"		else if (skillsNavigation==SKILLS_GO_FAST_NEXT)",
			"			skillsGoFastNext();",
			"		else if (skillsNavigation==SKILLS_GO_LAST)",
			"			skillsGoLast();",
			"		else if (skillsNavigation==CLOSE_SKILLS_POPUP)",
			"			closeCurrentSkill();",
			"		else if (skillsNavigation==CREATE_SKILL)",
			"			doInsertSkill();",
			"		skillsNavigation = SKILLS_NO_NAVIGATION;",
			"	}",
			"	",
			"	public void saveAndNavigateInsideSkills() {",
			"		validateCurrentSkill();",
			"		navigateInsideSkills();",
			"	}",
			"	",
			"	public void cancelAndNavigateInsideSkills() {",
			"		cancelCurrentSkill();",
			"		navigateInsideSkills();",
			"	}",
			"	",
			"	public String getCurrentSkillName() {",
			"		return currentSkillName;",
			"	}",
			"	",
			"	public void setCurrentSkillName(String currentSkillName) {",
			"		this.currentSkillName = currentSkillName;",
			"	}",
			"	",
			"	public boolean openSelectedSkills() {",
			"		openedSkills = new ArrayList<Skill>();",
			"		for (Skill skill : getSkills()) {",
			"			if (selectedSkills.containsKey(skill) && selectedSkills.get(skill)) {",
			"				openedSkills.add(skill);",
			"			}",
			"		}",
			"		skillsGoFirst();",
			"	}",
			"	",
			"	public boolean openAllSkills() {",
			"		openedSkills = new ArrayList<Skill>(getSkills());",
			"		skillsGoFirst();",
			"	}",
			"	",
			"	public boolean openSelectedCategories() {",
			"		openedCategories = new ArrayList<Category>();",
			"		for (Category category : getCategories()) {",
			"			if (selectedCategories.containsKey(category) && selectedCategories.get(category)) {",
			"				openedCategories.add(category);",
			"			}",
			"		}",
			"		categoriesGoFirst();",
			"	}",
			"	",
			"	public boolean openAllCategories() {",
			"		openedCategories = new ArrayList<Category>(getCategories());",
			"		categoriesGoFirst();",
			"	}",
			"	",
			"	public int getOperation() {",
			"		return operation;",
			"	}",
			"	",
			"	public void setOperation(int operation) {",
			"		this.operation = operation;",
			"	}",
			"	",
			"	public void cancelOperation() {",
			"		operation = OP_NONE;",
			"	}",
			"	",
			"	public void continueOperation() {",
			"		if (operation == OP_DELETE) {",
			"			if (targetCategoryForDeletion != null) {",
			"				deleteCategory(targetCategoryForDeletion);",
			"				targetCategoryForDeletion = null;",
			"			}",
			"			if (targetSkillForDeletion != null) {",
			"				deleteSkill(targetSkillForDeletion);",
			"				targetSkillForDeletion = null;",
			"			}",
			"		}",
			"		operation = OP_NONE;",
			"	}",
			"	",
			"	public boolean isConfirmRequested() {",
			"		return operation != OP_NONE;",
			"	}",
			"	",
			"	public void setConfirmRequested(boolean confirmRequested) {",
			"		if (!confirmRequested) {",
			"			cancelOperation();",
			"		}",
			"	}",
			"",
			"}"
		);
		TestUtil.assertText(page.getFaceletText(),
			"<!DOCTYPE composition PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"",
			"			\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",
			"<ui:composition xmlns=\"http://www.w3.org/1999/xhtml\"",
			"	xmlns:s=\"http://jboss.com/products/seam/taglib\"",
			"	xmlns:ui=\"http://java.sun.com/jsf/facelets\"",
			"	xmlns:f=\"http://java.sun.com/jsf/core\"",
			"	xmlns:h=\"http://java.sun.com/jsf/html\"",
			"	xmlns:ice=\"http://www.icesoft.com/icefaces/component\"",
			"	template=\"/layout/template.xhtml\">",
			"",
			"<ui:define name=\"body\">",
			"",
			"	<h:messages styleClass=\"message\"/>",
			"",
			"	<ice:form id=\"pageForm\">",
			"		<ice:panelGroup	styleClass=\"formBorderHighlight\">",
			"",
			"			<div class=\"dialog\">",
			"				<h:panelGroup>",
			"					<ice:dataTable id=\"categories\" value=\"#{editSkills.categories}\" var=\"category\"",
			"						rows=\"10\" resizable=\"true\" columnWidths=\"200px,100px\">",
			"						<ice:column>",
			"							<f:facet name=\"header\">Name</f:facet>",
			"							<ice:rowSelector value=\"#{editSkills.selectedCategories[category]}\" multiple=\"true\" />",
			"							<h:outputText value=\"#{category.name}\" />",
			"						</ice:column>",
			"						<ice:column>",
			"							<h:commandButton value=\"del\" action=\"#{editSkills.requestCategoryDeletion(category)}\" disabled=\"#{editSkills.currentCategoryVisible}\"/>",
			"						</ice:column>",
			"					</ice:dataTable>",
			"					<ice:dataPaginator for=\"categories\" fastStep=\"10\" paginator=\"true\" paginatorMaxPages=\"9\">",
			"						<f:facet name=\"first\">",
			"							<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-first.gif\" style=\"border:none;\"/>",
			"						</f:facet>",
			"						<f:facet name=\"last\">",
			"							<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-last.gif\" style=\"border:none;\" />",
			"						</f:facet>",
			"						<f:facet name=\"previous\">",
			"							<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-previous.gif\" style=\"border:none;\" />",
			"						</f:facet>",
			"						<f:facet name=\"next\">",
			"							<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-next.gif\" style=\"border:none;\" />",
			"						</f:facet>",
			"						<f:facet name=\"fastforward\">",
			"							<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-ff.gif\" style=\"border:none;\" />",
			"						</f:facet>",
			"						<f:facet name=\"fastrewind\">",
			"							<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-fr.gif\" style=\"border:none;\" />",
			"						</f:facet>",
			"					</ice:dataPaginator>",
			"					<h:panelGroup rendered=\"#{!editSkills.currentCategoryVisible}\">",
			"						<h:commandButton value=\"ouvrir\" action=\"#{editSkills.openSelectedCategories}\" rendered=\"#{!editSkills.currentCategoryVisible}\"/>",
			"						<h:commandButton value=\"ouvrir tout\" action=\"#{editSkills.openAllCategories}\" rendered=\"#{!editSkills.currentCategoryVisible}\"/>",
			"						<h:commandButton value=\"Creer\" action=\"#{editSkills.createCategory}\" rendered=\"#{!editSkills.currentCategoryVisible}\"/>",
			"					</h:panelGroup>",
			"				</h:panelGroup>",
			"				<h:panelGroup rendered=\"#{editSkills.currentCategoryVisible}\">",
			"					<div class=\"mask\"/>",
			"					<h:panelGroup style=\"position:relative\">",
			"						<s:validateAll>",
			"							<h:panelGrid columns=\"4\" rowClasses=\"prop\" columnClasses=\"name,value,name,value\">",
			"								<h:outputText value=\"Nom de la categorie : \" />",
			"								<s:decorate template=\"/layout/edit.xhtml\">",
			"									<h:inputText value=\"#{editSkills.currentCategoryName}\" size=\"30\"/>",
			"								</s:decorate>",
			"							</h:panelGrid>",
			"						</s:validateAll>",
			"						<h:panelGroup rendered=\"#{editSkills.categorySkillsVisible}\">",
			"							<h:panelGroup>",
			"								<ice:dataTable id=\"skills\" value=\"#{editSkills.skills}\" var=\"skill\"",
			"									rows=\"10\" resizable=\"true\" columnWidths=\"200px,100px\">",
			"									<ice:column>",
			"										<f:facet name=\"header\">Name</f:facet>",
			"										<ice:rowSelector value=\"#{editSkills.selectedSkills[skill]}\" multiple=\"true\" />",
			"										<h:outputText value=\"#{skill.name}\" />",
			"									</ice:column>",
			"									<ice:column>",
			"										<h:commandButton value=\"del\" action=\"#{editSkills.requestSkillDeletion(skill)}\" disabled=\"#{editSkills.currentSkillVisible}\"/>",
			"									</ice:column>",
			"								</ice:dataTable>",
			"								<ice:dataPaginator for=\"skills\" fastStep=\"10\" paginator=\"true\" paginatorMaxPages=\"9\">",
			"									<f:facet name=\"first\">",
			"										<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-first.gif\" style=\"border:none;\"/>",
			"									</f:facet>",
			"									<f:facet name=\"last\">",
			"										<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-last.gif\" style=\"border:none;\" />",
			"									</f:facet>",
			"									<f:facet name=\"previous\">",
			"										<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-previous.gif\" style=\"border:none;\" />",
			"									</f:facet>",
			"									<f:facet name=\"next\">",
			"										<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-next.gif\" style=\"border:none;\" />",
			"									</f:facet>",
			"									<f:facet name=\"fastforward\">",
			"										<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-ff.gif\" style=\"border:none;\" />",
			"									</f:facet>",
			"									<f:facet name=\"fastrewind\">",
			"										<ice:graphicImage url=\"/xmlhttp/css/xp/css-images/arrow-fr.gif\" style=\"border:none;\" />",
			"									</f:facet>",
			"								</ice:dataPaginator>",
			"								<h:panelGroup rendered=\"#{!editSkills.currentSkillVisible}\">",
			"									<h:commandButton value=\"ouvrir\" action=\"#{editSkills.openSelectedSkills}\" rendered=\"#{!editSkills.currentSkillVisible}\"/>",
			"									<h:commandButton value=\"ouvrir tout\" action=\"#{editSkills.openAllSkills}\" rendered=\"#{!editSkills.currentSkillVisible}\"/>",
			"									<h:commandButton value=\"Creer\" action=\"#{editSkills.createSkill}\" rendered=\"#{!editSkills.currentSkillVisible}\"/>",
			"								</h:panelGroup>",
			"							</h:panelGroup>",
			"							<h:panelGroup rendered=\"#{editSkills.currentSkillVisible}\">",
			"								<div class=\"mask\"/>",
			"								<h:panelGroup style=\"position:relative\">",
			"									<s:validateAll>",
			"										<h:panelGrid columns=\"4\" rowClasses=\"prop\" columnClasses=\"name,value,name,value\">",
			"											<h:outputText value=\"Nom de la competence : \" />",
			"											<s:decorate template=\"/layout/edit.xhtml\">",
			"												<h:inputText value=\"#{editSkills.currentSkillName}\" size=\"30\"/>",
			"											</s:decorate>",
			"										</h:panelGrid>",
			"									</s:validateAll>",
			"									<div class=\"actionButtons\">",
			"										<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-first.gif\" action=\"#{editSkills.skillsGoFirst}\"/>",
			"										<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-fr.gif\" action=\"#{editSkills.skillsGoFastPrevious}\"/>",
			"										<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-previous.gif\" action=\"#{editSkills.skillsGoPrevious}\"/>",
			"										<h:commandButton value=\"valider\" action=\"#{editSkills.validateCurrentSkill}\" />",
			"										<h:commandButton value=\"creer\" action=\"#{editSkills.insertSkill}\" rendered=\"#{editSkills.createSkillRendered}\"/>",
			"										<h:commandButton value=\"fermer\" action=\"#{editSkills.closeCurrentSkill}\"/>",
			"										<h:commandButton value=\"annuler\" immediate=\"true\" action=\"#{editSkills.cancelAndNavigateInsideSkills}\"/>",
			"										<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-next.gif\" action=\"#{editSkills.skillsGoNext}\"/>",
			"										<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-ff.gif\" action=\"#{editSkills.skillsGoFastNext}\"/>",
			"										<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-last.gif\" action=\"#{editSkills.skillsGoLast}\"/>",
			"									</div>",
			"								</h:panelGroup>",
			"							</h:panelGroup>",
			"						</h:panelGroup>",
			"						<div class=\"actionButtons\">",
			"							<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-first.gif\" action=\"#{editSkills.categoriesGoFirst}\"/>",
			"							<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-fr.gif\" action=\"#{editSkills.categoriesGoFastPrevious}\"/>",
			"							<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-previous.gif\" action=\"#{editSkills.categoriesGoPrevious}\"/>",
			"							<h:commandButton value=\"valider\" action=\"#{editSkills.validateCurrentCategory}\" />",
			"							<h:commandButton value=\"creer\" action=\"#{editSkills.insertCategory}\" rendered=\"#{editSkills.createCategoryRendered}\"/>",
			"							<h:commandButton value=\"fermer\" action=\"#{editSkills.closeCurrentCategory}\"/>",
			"							<h:commandButton value=\"annuler\" immediate=\"true\" action=\"#{editSkills.cancelAndNavigateInsideCategories}\"/>",
			"							<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-next.gif\" action=\"#{editSkills.categoriesGoNext}\"/>",
			"							<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-ff.gif\" action=\"#{editSkills.categoriesGoFastNext}\"/>",
			"							<h:commandButton image=\"/xmlhttp/css/xp/css-images/arrow-last.gif\" action=\"#{editSkills.categoriesGoLast}\"/>",
			"						</div>",
			"					</h:panelGroup>",
			"				</h:panelGroup>",
			"			</div>",
			"		</ice:panelGroup>",
			"		<ice:panelPopup modal=\"true\" draggable=\"true\" styleClass=\"popup\" rendered=\"#{editSkills.confirmRequested}\">",
			"			<f:facet name=\"header\">",
			"				<ice:panelGrid styleClass=\"title\" cellpadding=\"0\" cellspacing=\"0\" columns=\"2\">",
			"					<ice:outputText value=\"Confirmation\"/>",
			"				</ice:panelGrid>",
			"			</f:facet>",
			"			<f:facet name=\"body\">",
			"				<ice:panelGrid width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" column=\"1\" styleClass=\"body\">",
			"					<ice:outputText value=\"Voulez-vous supprimer la categorie : #{editSkills.targetCategoryForDeletion.name} ?\" rendered=\"#{editSkills.operation==1}\"/>", 
			"					<ice:outputText value=\"Voulez-vous supprimer la competence : #{editSkills.targetSkillForDeletion.name} ?\" rendered=\"#{editSkills.operation==1}\"/>",
			"					<div class=\"actionButtons\">",
			"						<h:commandButton value=\"valider\" action=\"#{editSkills.continueOperation}\"/>",
			"						<h:commandButton value=\"annuler\" immediate=\"true\" action=\"#{editSkills.cancelOperation}\"/>",
			"					</div>",
			"				</ice:panelGrid>",
			"			</f:facet>",
			"		</ice:panelPopup>",
			"		<ice:panelPopup modal=\"true\" draggable=\"true\" styleClass=\"popup\" rendered=\"#{editSkills.confirmCategoriesNavigationRequested}\">",
			"			<f:facet name=\"header\">",
			"				<ice:panelGrid styleClass=\"title\" cellpadding=\"0\" cellspacing=\"0\" columns=\"2\">",
			"					<ice:outputText value=\"Confirmation\"/>",
			"				</ice:panelGrid>",
			"			</f:facet> ",
			"			<f:facet name=\"body\">",
			"				<ice:panelGrid width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" column=\"1\" styleClass=\"body\">",
			"					<ice:outputText value=\"Des donnees ont ete modifiees. Voulez vous continuer ?\" />",
			"					<div class=\"actionButtons\">",
			"						<h:commandButton value=\"continuer\" action=\"#{editSkills.cancelAndNavigateInsideCategories}\"/>",
			"						<h:commandButton value=\"sauver\" action=\"#{editSkills.saveAndNavigateInsideCategories}\"/>",
			"						<h:commandButton value=\"annuler\" immediate=\"true\" action=\"#{editSkills.cancelCategoriesNavigation}\"/>",
			"					</div>",
			"				</ice:panelGrid>",
			"			</f:facet>",
			"		</ice:panelPopup>",
			"		<ice:panelPopup modal=\"true\" draggable=\"true\" styleClass=\"popup\" rendered=\"#{editSkills.confirmCategoriesNavigationRequested}\">",
			"			<f:facet name=\"header\">",
			"				<ice:panelGrid styleClass=\"title\" cellpadding=\"0\" cellspacing=\"0\" columns=\"2\">",
			"					<ice:outputText value=\"Confirmation\"/>",
			"				</ice:panelGrid>",
			"			</f:facet> ",
			"			<f:facet name=\"body\">",
			"				<ice:panelGrid width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" column=\"1\" styleClass=\"body\">",
			"					<ice:outputText value=\"Des donnees ont ete modifiees. Voulez vous continuer ?\" />",
			"					<div class=\"actionButtons\">",
			"						<h:commandButton value=\"continuer\" action=\"#{editSkills.cancelAndNavigateInsideCategories}\"/>",
			"						<h:commandButton value=\"sauver\" action=\"#{editSkills.saveAndNavigateInsideCategories}\"/>",
			"						<h:commandButton value=\"annuler\" immediate=\"true\" action=\"#{editSkills.cancelCategoriesNavigation}\"/>",
			"					</div>",
			"				</ice:panelGrid>",
			"			</f:facet>",
			"		</ice:panelPopup>",
			"	</ice:form>",
			"</ui:define>",
			"</ui:composition>"
		);
	}

}
